<?php
App::uses('AppModel', 'Model');
/**
 * Season Model
 *
 * @property Anime $Anime
 * @property Situation $Situation
 * @property Rating $Rating
 */
class Season extends AppModel {

    /**
     * beforeSave method
     * Executa antes de salvar os dados no banco.
     */
    public function dateFormatBeforeSave($dataString){
        list($day,$month,$year) = explode('/',$dataString);
        $dataString =  $year.'-'.$month.'-'.$day;
        return $dataString;
    }

    public function beforeSave($options = array())
    {
        if(!empty($this->data['Season']['release_date']))
        {
            $this->data['Season']['release_date'] = $this->dateFormatBeforeSave($this->data['Season']['release_date']);
        }
        return true;
    }
    /**
     * afterFind method
     * Converte a data que vem do banco para o formato
     * desejado pela função dateFormatAfterFind.
     *
     */
    public function afterFind($results, $primary = false)
    {
        foreach ($results as $key => $val) {
            if (isset($val['Season']['release_date'])) {
                $results[$key]['Season']['release_date'] = $this->dateFormatAfterFind($val['Season']['release_date']);
            }
        }
        /*if(!empty($results[0]['Season']['release_date']))
        {
            $results[0]['Season']['release_date'] = $this->dateFormatAfterFind($results[0]['Season']['release_date']);
        }*/
        return $results;
    }

    public function dateFormatAfterFind($dateString=null) {
        if(!empty($dateString)){
            list($year,$month,$day) = explode('-',$dateString);
            $dateString =  $day.'/'.$month.'/'.$year;
        }
        return $dateString;
    }

    /**
     * Validation rules
     *
     * @var array
     */
    public $validate = array(
        'anime_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Escolha um Anime',
                //'allowEmpty' => false,
                'required' => true,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'situation_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Escolha uma Situação',
                //'allowEmpty' => false,
                'required' => true,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'rating_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Faça uma avaliação',
                //'allowEmpty' => false,
                'required' => true,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'release_year' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Informe o ano de lançamento.',
                //'allowEmpty' => false,
                'required' => true,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'dow_episodes' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Informe os episodios baixados.',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'full_episodes' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                'message' => 'Informe o total de episodios',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'start_season'=>array(
            'numeric'=>array(
                'rule'=>array('numeric','notempty'),
                'message'=>'Informe o episodio inicial da temporada ou arco.',
                'allowEmpty'=>false,
                'required'=>true,
            ),
        ),
        'end_season'=>array(
            'numeric'=>array(
                'rule'=>array('numeric','notempty'),
                'message'=>'Informe o episodio final da temporada ou arco.',
                'allowEmpty'=>false,
                'required'=>true,
            ),
        ),
        'status' => array(
            'boolean' => array(
                'rule' => array('boolean'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Anime' => array(
            'className' => 'Anime',
            'foreignKey' => 'anime_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Situation' => array(
            'className' => 'Situation',
            'foreignKey' => 'situation_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Rating' => array(
            'className' => 'Rating',
            'foreignKey' => 'rating_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}
